<?php
/****************多维数组转一维数组*************************/
function arrToOne($multi) {
    $arr = array();
    foreach ($multi as $key => $val) {
        if( is_array($val) ) {
            $arr = array_merge($arr, arrToOne($val));
        } else {
            $arr[] = $val;
        }
    }
    return $arr;
}
/****************跨域*************************/
function kuayu()
{
    header('content-type:application:json;charset=utf8');
    header('Access-Control-Allow-Origin:*');
    header('Access-Control-Allow-Methods:POST');
    header('Access-Control-Allow-Headers:x-requested-with,content-type');
}
/****************生成guid*************************/
function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $hyphen = chr(45);// "-"
    $uuid =substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12);
//         chr(123)
//         ..chr(125);
    return $uuid;
}

/****************查用户的地址*************************/
function query_address($uid){
    $address = D('Users_address');
    $address_id = D('Users')->where('id='.$uid)->getfield('address_id');
    $info['default_address'] = $address->find($address_id);
    $info['address'] = $address->where('user_id='.$uid)->select();
    return $info;
}

/****************查用户等级会员*************************/
function query_user_lever($uid)
{
    $users = D('Users')->find($uid);
    if(intval($users['consume']) <= 10000){
        $data['fraction']= 0.02;//积分率
        $data['member_name'] = '铁牌会员';
    }elseif(intval($users['consume']) > 10000 && intval($users['consume']) <= 20000){
        $data['fraction'] = 0.04;//积分率
        $data['member_name'] = '铜牌会员';
    }elseif(intval($users['consume']) > 20000 && intval($users['consume']) <= 40000){
        $data['fraction'] = 0.06;//积分率
        $data['member_name'] = '银牌会员';
    }elseif(intval($users['consume']) > 40000 && intval($users['consume']) <= 80000){
        $data['fraction'] = 0.08;//积分率
        $data['member_name'] = '金牌会员';
    }else{
        $data['fraction'] = 0.1;//积分率
        $data['member_name'] = '钻石牌会员';
    }
    return $data;
}

/****************查指定购物车的商品*************************/
function query_cart_good($cart_id,$uid){
    $sql = "select g.id,g.cn,g.product_name,g.img1,g.shop_price,c.number from wlg_cart as c right JOIN wlg_goods as g on c.good_id=g.id where c.uid=".$uid." and c.id in (".$cart_id.");";
//        $sql = "select g.id,g.cn,g.product_name,g.shop_price,c.number from wlg_cart as c right JOIN wlg_goods as g on c.good_id=g.id where c.uid=".$data['uid']." and c.id in (".$data['cart_id'].");";
    $goods = M()->query($sql);
    return $goods;
}

/**************s算一共有多少下线****************/
function query_offline($id,$deep=0){
    static $num = 0;
    static $arr = array();
    $data = D('Users')->where('parent_id='.$id)->select();
    $num += count($data);
    foreach($data as $k=>$v)
    {
        $arr[] = $v;

        if($deep<3){
            query_offline($v['id'],$deep+1);
        }
    }
    $data = array('users'=>$arr,'num'=>$num);
    return $data;
}
/**************查询个人信息****************/
function query_user($id)
{
    $data = D('Users')->find($id);
    return $data;
}

/**************添加配置文件****************/
function arrToFile($filename = '', $arr = '') {
    if (is_array ( $arr )) {
        // 数组转字符串
        $con = var_export ( $arr, true );
    } else {
        $con = $arr;
    }
    $con = "<?php\nreturn $con;\n?>";
    file_put_contents($filename, $con);
}

/**************生成二维码***************/
function qrcode($url,$size=4){
    Vendor('Phpqrcode.phpqrcode');
    if (strpos($url, 'http')===false) {
        $url='http://'.$url;
    }
    QRcode::png($url,false,QR_ECLEVEL_L,$size,2,false,0xFFFFFF,0x000000);
}

// function checkorderstatus($ordid){
//     $Ord=M('Orderlist');
//     $ordstatus=$Ord->where('ordid='.$ordid)->getField('ordstatus');
//     if($ordstatus==1){
//         return ture;
//     }else{
//         return false;
//     }
//  }


?>  